package com.iw.sms.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alvin.sms.model.SendSmsVO;
import com.iw.adoms.action.BaseAction;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;
import com.iw.sms.entity.Sms;
import com.iw.sms.quartz.AutoReciveQuartz;
import com.iw.sms.quartz.AutoReplyQuartz;
import com.iw.sms.service.AlvinSmsService;
import com.iw.sms.service.AutoReciveSmsService;
import com.iw.sms.service.AutoReplyService;
import com.iw.sms.service.SmsService;

@Controller
@RequestMapping("/sms")
public class AlvinSmsAction extends BaseAction {

	private static final Logger LOGGER = Logger.getLogger(AlvinSmsAction.class);

	@Autowired
	private AlvinSmsService alvinSmsService;
	@Autowired
	private SmsService smsService;
	@Autowired
	private AutoReciveSmsService autoReciveSmsService;
	@Autowired
	private AutoReplyService autoReplyService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<Sms> pageData = new PageData<Sms>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = smsService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping("/delete/{id}")
	public String deleteSms(HttpServletRequest request, @PathVariable("id") Integer id) {
		smsService.delete(id);
		return "redirect:/sms/list";
	}

	@RequestMapping(value = "/send", method = RequestMethod.GET)
	public String send(Model model, HttpServletRequest request) {
		return "/sms/send";
	}

	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public String send(Model model, HttpServletRequest request, SendSmsVO sendVO) {
		String msg = "";
		try {
			boolean flag = alvinSmsService.sendSms(sendVO);
			if (flag) {
				msg = "短信发送成功！";
			} else {
				msg = "系统短信发送服务已经关闭！";
				model.addAttribute("sendVO", sendVO);
			}
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			msg = "短信发送失败！";
			model.addAttribute("sendVO", sendVO);
		}
		model.addAttribute("msg", msg);
		return "/sms/send";
	}

	@RequestMapping(value = "/balance")
	public String balance(Model model) {
		try {
			Integer balance = alvinSmsService.checkBalance();
			model.addAttribute("balance", balance);
		} catch (Exception e) {
			LOGGER.error(e.getCause());
		}
		return "/sms/balance";
	}

	@RequestMapping(value = "/setting")
	public String setting(Model model) {
		model.addAttribute("rcIsRun", AutoReciveQuartz.isRun);
		model.addAttribute("rpIsRun", AutoReplyQuartz.isRun);
		model.addAttribute("smsIsRun", AlvinSmsService.isRun);
		return "/sms/setting";
	}

	@RequestMapping(value = "/startRecive")
	@ResponseBody
	public boolean startRecive(Model model) {
		try {
			autoReciveSmsService.startReciveSms();
			return true;
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			return false;
		}
	}

	@RequestMapping(value = "/stopRecive")
	@ResponseBody
	public boolean stopRecive(Model model) {
		try {
			autoReciveSmsService.stopReciveSms();
			return true;
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			return false;
		}
	}

	@RequestMapping(value = "/startReply")
	@ResponseBody
	public boolean startReply(Model model) {
		try {
			autoReplyService.startReply();
			return true;
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			return false;
		}
	}

	@RequestMapping(value = "/stopReply")
	@ResponseBody
	public boolean stopReply(Model model) {
		try {
			autoReplyService.stopReply();
			return true;
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			return false;
		}
	}

	@RequestMapping(value = "/startSend")
	@ResponseBody
	public boolean startSend(Model model) {
		try {
			alvinSmsService.startSend();
			return true;
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			return false;
		}
	}

	@RequestMapping(value = "/stopSend")
	@ResponseBody
	public boolean stopSend(Model model) {
		try {
			alvinSmsService.stopSend();
			return true;
		} catch (Exception e) {
			LOGGER.error(e.getCause());
			return false;
		}
	}

}
